//
//  LCCouponViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/20.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCCouponViewController.h"
#import "LCCouponService.h"
#import "LCCouponAdvertModel.h"

//轮播图
#import "HQFlowView.h"
#import "LCPageControlView.h"

#import "XWXAllXieYiController.h"

#import "LCCouponAdvertViewController.h"
#import "LCCouponListViewController.h"

@interface LCCouponViewController ()<HQFlowViewDelegate,HQFlowViewDataSource,UIScrollViewDelegate, UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIView *navBgView;

@property (weak, nonatomic) IBOutlet UITextField *searchTfl;

@property (weak, nonatomic) IBOutlet UIButton *hotBtn;
@property (weak, nonatomic) IBOutlet UIButton *liveBtn;
@property (weak, nonatomic) IBOutlet UIButton *videoBtn;
@property (weak, nonatomic) IBOutlet UIButton *toolBtn;

@property (weak, nonatomic) IBOutlet UIView *headTagView;

@property (weak, nonatomic) IBOutlet LCBaseScrollView *baseScrollView;

@property (nonatomic, assign) BOOL canScroll;

@property (weak, nonatomic) IBOutlet UIView *advertBgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *advertBgViewH;

@property (nonatomic, strong) LCPageControlView *advertPageC;
@property (strong, nonatomic) HQFlowView *adFlowView;

@property (weak, nonatomic) IBOutlet LCContentScrollView *itemScrollView;

@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentViewH;

@property (strong, nonatomic)NSMutableArray * advertControllers;

@property (strong, nonatomic)NSArray * advertArr;

@property (strong, nonatomic)LCCouponListViewController * listVC;

@end

@implementation LCCouponViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNav];
    [self initUI];
    [self addChildController];
    [self getAdVertData];
}

- (void)setNav {
    self.navBgView.backgroundColor = [UIColor gradientChange_ColorWithSize:CGSizeMake(ScreenW, NavgationHeight) direction:GradientChangeColorDirectionLevel startColor:[UIColor colorWithHexString:@"#00AAFE"] endColor:[UIColor colorWithHexString:@"#7616E3"]];
}

-(void)initUI {
    self.canScroll = YES;
    
    [LCNotiCenter addObserver:self selector:@selector(couponListControllerLeaveTop:) name:LCCouponListControllerLeaveTopNoti object:nil];
    
    self.baseScrollView.width = ScreenW;
    
    self.advertBgView.width = ScreenW-34;
    self.advertBgView.height = 139*proW;
    self.advertBgViewH.constant = 139*proW;
    
    [self.advertBgView addSubview:self.adFlowView];
    [self.advertBgView addSubview:self.advertPageC];
    
    [self.adFlowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.bottom.equalTo(self.advertBgView);
    }];
    [self.advertPageC mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(16);
        make.right.left.bottom.equalTo(self.advertBgView);
    }];
    
    self.itemScrollView.width = ScreenW;
    self.itemScrollView.height = 180;
    self.itemScrollView.contentSize = CGSizeMake(4*ScreenW, 0);
        
    self.contentView.width = ScreenW;
    self.contentView.height = ScreenH - NavgationHeight - TabBarHeight - 44;
    self.contentViewH.constant = ScreenH - NavgationHeight - TabBarHeight - 44;
}

-(HQFlowView *)adFlowView {
    if (!_adFlowView) {
        _adFlowView = [[HQFlowView alloc] init];
        _adFlowView.delegate = self;
        _adFlowView.dataSource = self;
        _adFlowView.orginPageCount = 2;
        _adFlowView.isOpenAutoScroll = YES;
        _adFlowView.autoTime = 3.0;
        _adFlowView.orientation = HQFlowViewOrientationHorizontal;
    }
    return _adFlowView;
}

-(LCPageControlView *)advertPageC {
    if (!_advertPageC) {
        _advertPageC = [[LCPageControlView alloc] init];
        _advertPageC.otherDotColor = [UIColor colorWithHexString:@"#000000" alpha:0.2];
        _advertPageC.currentDotColor = [UIColor colorWithHexString:@"#000000" alpha:0.2];
        _advertPageC.dotSpace = 5;
        _advertPageC.dotHeight = 4;
        _advertPageC.otherDotWidth = 4;
        _advertPageC.currentDotWidth = 16;
        _advertPageC.cornerRadius = 2;
        
        self.adFlowView.pageControl.currentPage = 0;
        self.adFlowView.pageControl = _advertPageC;
    }
    return _advertPageC;
}

- (void)addChildController {
    self.advertControllers = [NSMutableArray array];
    for (int i = 0; i < 4; i++) {
        LCCouponAdvertViewController * pageVC = [LCCouponAdvertViewController xx_instantiateFromStoryboardNamed:@"Coupon"];
        pageVC.groupname = [self groupNameForPage:i];
        pageVC.view.frame = CGRectMake(ScreenW * i, 0, ScreenW, CGRectGetHeight(self.itemScrollView.frame));
        [self.itemScrollView addSubview:pageVC.view];
        [self addChildViewController:pageVC];
        [self.advertControllers addObject:pageVC];
    }
    
    self.listVC = [LCCouponListViewController xx_instantiateFromStoryboardNamed:@"Coupon"];
    self.listVC.view.frame = CGRectMake(0, 0, ScreenW, CGRectGetHeight(self.contentView.frame));
    [self.contentView addSubview:self.listVC.view];
    [self addChildViewController:self.listVC];
}

- (NSString *)groupNameForPage:(int)page {
    switch (page) {
        case 0: {
            return @"热门";
        } break;
        case 1: {
            return @"生活";
        } break;
        case 2: {
            return @"影音";
        } break;
        default: {
            return @"工具";
        } break;
    }
}


- (void)getAdVertData {
    [LCCouponService getAdvertWithAdvertname:@"首页焦点图" Block:^(BOOL isSuccess, NSArray<LCCouponAdvertModel *> *list) {
        if (isSuccess) {
            self.advertArr = list;
            [self.adFlowView reloadData];
        }
    }];
}

- (IBAction)searchBtnAction:(UIButton *)sender {
    [self.view endEditing:YES];
}

- (void)couponListControllerLeaveTop:(NSNotification *)notif {
    self.canScroll = [notif.object boolValue];
}

#pragma mark JQFlowViewDelegate
- (CGSize)sizeForPageInFlowView:(HQFlowView *)flowView {
    return CGSizeMake(ScreenW-34, 139*proW);
}

- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex {
    LCCouponAdvertModel * advert = self.advertArr[subIndex];
    if ([NSString urlValidation:advert.strlinks]) {
        XWXAllXieYiController *webVC = [XWXAllXieYiController new];
        webVC.tileStr = advert.listname;
        webVC.urlStr = advert.strlinks;
        webVC.isPushFrom = YES;
        webVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:webVC animated:YES];
    } else {
        if (subIndex == 2) {
            [LCNotiCenter postNotificationName:LCTaskControllerSetIndexNoti object:@"0"];
            self.tabBarController.selectedIndex = 1;
        } else if (subIndex == 3) {
            [LCNotiCenter postNotificationName:LCTaskControllerSetIndexNoti object:@"2"];
            self.tabBarController.selectedIndex = 1;
        }
    }
}

#pragma mark JQFlowViewDatasource
- (NSInteger)numberOfPagesInFlowView:(HQFlowView *)flowView {
    return self.advertArr.count;
}

- (HQIndexBannerSubview *)flowView:(HQFlowView *)flowView cellForPageAtIndex:(NSInteger)index {
    HQIndexBannerSubview *bannerView = (HQIndexBannerSubview *)[flowView dequeueReusableCell];
    if (!bannerView) {
        bannerView = [[HQIndexBannerSubview alloc] initWithFrame:CGRectMake(0, 0, self.adFlowView.frame.size.width, self.adFlowView.frame.size.height)];
        bannerView.layer.cornerRadius = 5;
        bannerView.layer.masksToBounds = YES;
        bannerView.coverView.backgroundColor = [UIColor darkGrayColor];
    }
    LCCouponAdvertModel * advert = self.advertArr[index];
    [bannerView.mainImageView sd_setImageWithURL:[LCFunction XWX_URLImg:advert.strimage]];
    return bannerView;
}

- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(HQFlowView *)flowView {
    [self.adFlowView.pageControl setCurrentPage:pageNumber];
}
     
     
- (IBAction)hotBtnAction:(UIButton *)sender {
    sender.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    self.liveBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.videoBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.toolBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.headTagView.centerX = sender.centerX;
    [self.itemScrollView setContentOffset:CGPointMake(ScreenW * 0, 0) animated:YES];
}

- (IBAction)liveBtnAction:(UIButton *)sender {
    sender.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    self.hotBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.videoBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.toolBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.headTagView.centerX = sender.centerX;
    [self.itemScrollView setContentOffset:CGPointMake(ScreenW * 1, 0) animated:YES];
}

- (IBAction)videoBtnAction:(UIButton *)sender {
    sender.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    self.hotBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.liveBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.toolBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.headTagView.centerX = sender.centerX;
    [self.itemScrollView setContentOffset:CGPointMake(ScreenW * 2, 0) animated:YES];
}

- (IBAction)toolBtnAction:(UIButton *)sender {
    sender.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    self.hotBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.liveBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.videoBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.headTagView.centerX = sender.centerX;
    [self.itemScrollView setContentOffset:CGPointMake(ScreenW * 3, 0) animated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.itemScrollView) {
        CGFloat offsetX = scrollView.contentOffset.x;
        NSInteger page = offsetX/ScreenW;
        if (page == 0) {
            self.hotBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
            self.liveBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            self.videoBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            self.toolBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            self.headTagView.centerX = self.hotBtn.centerX;
        } else if (page == 1) {
            self.liveBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
            self.hotBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            self.videoBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            self.toolBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            self.headTagView.centerX = self.liveBtn.centerX;
        } else if (page == 2) {
            self.videoBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
            self.hotBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            self.toolBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            self.liveBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            self.headTagView.centerX = self.videoBtn.centerX;
        } else if (page == 3) {
            self.toolBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
            self.hotBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            self.liveBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            self.videoBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            self.headTagView.centerX = self.toolBtn.centerX;
        }
    } else if (scrollView == self.baseScrollView) {
        CGFloat maxOffsetY = 139*proW + 9 + 180;
        CGFloat offsetY = scrollView.contentOffset.y;
        if (offsetY >= maxOffsetY) {
            _canScroll = NO;
            scrollView.contentOffset = CGPointMake(0, maxOffsetY);
            [[NSNotificationCenter defaultCenter] postNotificationName:LCCouponListControllerGoTopNoti object:@(YES)];
        } else {
            if (!_canScroll) {
                scrollView.contentOffset = CGPointMake(0, maxOffsetY);
            }
        }
    }
}

@end
